{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.externals import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-   \n",
    "import os \n",
    "  \n",
    "def file_m(file_dir):  \n",
    "    L=[]  \n",
    "    for root, dirs, files in os.walk(file_dir): \n",
    "        for file in files: \n",
    "            if os.path.splitext(file)[1] == '.m': \n",
    "                L.append(file) \n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-   \n",
    "import os \n",
    "  \n",
    "def file_txt(file_dir):  \n",
    "    L=[]  \n",
    "    for root, dirs, files in os.walk(file_dir): \n",
    "        for file in files: \n",
    "            if os.path.splitext(file)[1] == '.txt': \n",
    "                L.append(file) \n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jeep指南者.txt',\n",
       " 'Jeep牧马人.txt',\n",
       " 'MG3.txt',\n",
       " 'MG5.txt',\n",
       " 'MG6.txt',\n",
       " 'MINI.txt',\n",
       " '一汽威乐.txt',\n",
       " '三菱帕杰罗.txt',\n",
       " '三菱欧蓝德.txt',\n",
       " '三菱翼神.txt',\n",
       " '三菱蓝瑟.txt',\n",
       " '上汽大通G10.txt',\n",
       " '东南V3菱悦.txt',\n",
       " '东风帅客.txt',\n",
       " '东风风光330.txt',\n",
       " '东风风神H30.txt',\n",
       " '东风风神S30.txt',\n",
       " '东风风行景逸.txt',\n",
       " '东风风行菱智.txt',\n",
       " '中华骏捷.txt',\n",
       " '中华骏捷FRV.txt',\n",
       " '中华骏捷FSV.txt',\n",
       " '丰田RAV4荣放.txt',\n",
       " '丰田一汽丰田RAV4.txt',\n",
       " '丰田凯美瑞.txt',\n",
       " '丰田卡罗拉.txt',\n",
       " '丰田威驰.txt',\n",
       " '丰田普拉多.txt',\n",
       " '丰田汉兰达.txt',\n",
       " '丰田皇冠.txt',\n",
       " '丰田花冠.txt',\n",
       " '丰田锐志.txt',\n",
       " '丰田雅力士.txt',\n",
       " '丰田雷凌.txt',\n",
       " '五菱汽车五菱之光.txt',\n",
       " '五菱汽车五菱宏光.txt',\n",
       " '五菱汽车五菱荣光.txt',\n",
       " '五菱汽车五菱荣光V.txt',\n",
       " '五菱汽车五菱荣光小卡.txt',\n",
       " '众泰T600.txt',\n",
       " '依维柯宝迪.txt',\n",
       " '依维柯得意.txt',\n",
       " '保时捷Cayenne.txt',\n",
       " '保时捷Macan.txt',\n",
       " '克莱斯勒300C.txt',\n",
       " '凯迪拉克ATS-L.txt',\n",
       " '凯迪拉克CTS.txt',\n",
       " '凯迪拉克SLS赛威.txt',\n",
       " '凯迪拉克SRX.txt',\n",
       " '凯迪拉克XTS.txt',\n",
       " '别克GL8.txt',\n",
       " '别克凯越.txt',\n",
       " '别克君威.txt',\n",
       " '别克君越.txt',\n",
       " '别克威朗.txt',\n",
       " '别克昂科威.txt',\n",
       " '别克昂科拉.txt',\n",
       " '别克英朗.txt',\n",
       " '北京汽车E系列.txt',\n",
       " '北汽威旺306.txt',\n",
       " '北汽威旺M20.txt',\n",
       " '北汽幻速S3.txt',\n",
       " '吉利汽车吉利GX7.txt',\n",
       " '吉利汽车帝豪.txt',\n",
       " '吉利汽车熊猫.txt',\n",
       " '吉利汽车经典帝豪.txt',\n",
       " '吉利汽车远景.txt',\n",
       " '吉利汽车金刚.txt',\n",
       " '启辰D50.txt',\n",
       " '哈弗H1.txt',\n",
       " '哈弗H2.txt',\n",
       " '哈弗H3.txt',\n",
       " '哈弗H5.txt',\n",
       " '哈弗H6.txt',\n",
       " '大众Passat领驭.txt',\n",
       " '大众POLO.txt',\n",
       " '大众一汽-大众CC.txt',\n",
       " '大众凌渡.txt',\n",
       " '大众宝来.txt',\n",
       " '大众尚酷.txt',\n",
       " '大众帕萨特.txt',\n",
       " '大众捷达.txt',\n",
       " '大众朗行.txt',\n",
       " '大众朗逸.txt',\n",
       " '大众桑塔纳.txt',\n",
       " '大众桑塔纳3000.txt',\n",
       " '大众桑塔纳志俊.txt',\n",
       " '大众桑塔纳经典.txt',\n",
       " '大众甲壳虫.txt',\n",
       " '大众迈腾.txt',\n",
       " '大众途安.txt',\n",
       " '大众途观.txt',\n",
       " '大众途锐.txt',\n",
       " '大众速腾.txt',\n",
       " '大众高尔夫.txt',\n",
       " '奇瑞A3.txt',\n",
       " '奇瑞A5.txt',\n",
       " '奇瑞E3.txt',\n",
       " '奇瑞E5.txt',\n",
       " '奇瑞QQ3.txt',\n",
       " '奇瑞瑞虎.txt',\n",
       " '奇瑞瑞虎3.txt',\n",
       " '奇瑞瑞虎5.txt',\n",
       " '奇瑞风云2.txt',\n",
       " '奔腾B70.txt',\n",
       " '奔腾X80.txt',\n",
       " '奔腾全新奔腾B50.txt',\n",
       " '奔驰A级.txt',\n",
       " '奔驰B级.txt',\n",
       " '奔驰C级.txt',\n",
       " '奔驰E级.txt',\n",
       " '奔驰GLA.txt',\n",
       " '奔驰GLK级.txt',\n",
       " '奔驰M级.txt',\n",
       " '奔驰R级.txt',\n",
       " '奔驰S级.txt',\n",
       " '奔驰唯雅诺.txt',\n",
       " '奥迪A3.txt',\n",
       " '奥迪A4.txt',\n",
       " '奥迪A4L.txt',\n",
       " '奥迪A5.txt',\n",
       " '奥迪A6.txt',\n",
       " '奥迪A6L.txt',\n",
       " '奥迪A8.txt',\n",
       " '奥迪Q3.txt',\n",
       " '奥迪Q5.txt',\n",
       " '奥迪Q7.txt',\n",
       " '奥迪TT.txt',\n",
       " '宝马1系.txt',\n",
       " '宝马3系.txt',\n",
       " '宝马5系.txt',\n",
       " '宝马7系.txt',\n",
       " '宝马X1.txt',\n",
       " '宝马X3.txt',\n",
       " '宝马X5.txt',\n",
       " '宝马X6.txt',\n",
       " '宝骏560.txt',\n",
       " '宝骏630.txt',\n",
       " '宝骏730.txt',\n",
       " '宝骏乐驰.txt',\n",
       " '广汽传祺传祺GS4.txt',\n",
       " '广汽传祺传祺GS5.txt',\n",
       " '捷豹XF.txt',\n",
       " '斯巴鲁傲虎.txt',\n",
       " '斯巴鲁森林人.txt',\n",
       " '斯柯达昊锐.txt',\n",
       " '斯柯达明锐.txt',\n",
       " '斯柯达昕锐.txt',\n",
       " '斯柯达晶锐.txt',\n",
       " '斯柯达速派.txt',\n",
       " '日产NV200.txt',\n",
       " '日产天籁.txt',\n",
       " '日产奇骏.txt',\n",
       " '日产轩逸.txt',\n",
       " '日产逍客.txt',\n",
       " '日产阳光.txt',\n",
       " '日产颐达.txt',\n",
       " '日产骊威.txt',\n",
       " '日产骐达.txt',\n",
       " '本田CR-V.txt',\n",
       " '本田XR-V.txt',\n",
       " '本田凌派.txt',\n",
       " '本田奥德赛.txt',\n",
       " '本田思域.txt',\n",
       " '本田思迪.txt',\n",
       " '本田思铂睿.txt',\n",
       " '本田杰德.txt',\n",
       " '本田歌诗图.txt',\n",
       " '本田缤智.txt',\n",
       " '本田艾力绅.txt',\n",
       " '本田锋范.txt',\n",
       " '本田锋范经典.txt',\n",
       " '本田雅阁.txt',\n",
       " '本田飞度.txt',\n",
       " '标致2008.txt',\n",
       " '标致206.txt',\n",
       " '标致207.txt',\n",
       " '标致3008.txt',\n",
       " '标致301.txt',\n",
       " '标致307.txt',\n",
       " '标致308.txt',\n",
       " '标致408.txt',\n",
       " '标致508.txt',\n",
       " '比亚迪F0.txt',\n",
       " '比亚迪F3.txt',\n",
       " '比亚迪F3R.txt',\n",
       " '比亚迪F6.txt',\n",
       " '比亚迪G3.txt',\n",
       " '比亚迪G6.txt',\n",
       " '比亚迪L3.txt',\n",
       " '比亚迪S6.txt',\n",
       " '比亚迪S7.txt',\n",
       " '比亚迪唐.txt',\n",
       " '比亚迪秦.txt',\n",
       " '比亚迪速锐.txt',\n",
       " '江淮和悦.txt',\n",
       " '江淮瑞风.txt',\n",
       " '江淮瑞风M2.txt',\n",
       " '江淮瑞风S3.txt',\n",
       " '沃尔沃S40.txt',\n",
       " '沃尔沃XC60.txt',\n",
       " '海马普力马.txt',\n",
       " '海马海福星.txt',\n",
       " '海马福美来.txt',\n",
       " '现代伊兰特.txt',\n",
       " '现代全新胜达.txt',\n",
       " '现代北京现代ix25.txt',\n",
       " '现代北京现代ix35.txt',\n",
       " '现代名图.txt',\n",
       " '现代悦动.txt',\n",
       " '现代新胜达.txt',\n",
       " '现代朗动.txt',\n",
       " '现代瑞纳.txt',\n",
       " '现代索纳塔.txt',\n",
       " '现代索纳塔八.txt',\n",
       " '现代途胜.txt',\n",
       " '现代雅绅特.txt',\n",
       " '福特嘉年华.txt',\n",
       " '福特福克斯.txt',\n",
       " '福特福睿斯.txt',\n",
       " '福特经典全顺.txt',\n",
       " '福特翼搏.txt',\n",
       " '福特翼虎.txt',\n",
       " '福特蒙迪欧-致胜.txt',\n",
       " '福特蒙迪欧.txt',\n",
       " '福特锐界.txt',\n",
       " '福田风景.txt',\n",
       " '英菲尼迪G系.txt',\n",
       " '荣威350.txt',\n",
       " '荣威550.txt',\n",
       " '荣威750.txt',\n",
       " '菲亚特菲翔.txt',\n",
       " '起亚K2.txt',\n",
       " '起亚K3.txt',\n",
       " '起亚K4.txt',\n",
       " '起亚K5.txt',\n",
       " '起亚佳乐.txt',\n",
       " '起亚智跑.txt',\n",
       " '起亚狮跑.txt',\n",
       " '起亚福瑞迪.txt',\n",
       " '起亚秀尔.txt',\n",
       " '起亚索兰托.txt',\n",
       " '起亚赛拉图.txt',\n",
       " '起亚锐欧.txt',\n",
       " '路虎揽胜极光.txt',\n",
       " '路虎神行者2.txt',\n",
       " '路虎第四代发现.txt',\n",
       " '道奇酷威.txt',\n",
       " '金杯海狮.txt',\n",
       " '金杯阁瑞斯.txt',\n",
       " '铃木北斗星.txt',\n",
       " '铃木奥拓.txt',\n",
       " '铃木雨燕.txt',\n",
       " '长城C30.txt',\n",
       " '长城C50.txt',\n",
       " '长城M2.txt',\n",
       " '长城M4.txt',\n",
       " '长城炫丽.txt',\n",
       " '长安CS35.txt',\n",
       " '长安CS75.txt',\n",
       " '长安CX20.txt',\n",
       " '长安商用欧诺.txt',\n",
       " '长安商用金牛星.txt',\n",
       " '长安商用长安之星2.txt',\n",
       " '长安奔奔.txt',\n",
       " '长安奔奔MINI.txt',\n",
       " '长安悦翔.txt',\n",
       " '长安悦翔V3.txt',\n",
       " '长安逸动.txt',\n",
       " '陆风X7.txt',\n",
       " '雪佛兰乐风.txt',\n",
       " '雪佛兰乐驰.txt',\n",
       " '雪佛兰乐骋.txt',\n",
       " '雪佛兰创酷.txt',\n",
       " '雪佛兰景程.txt',\n",
       " '雪佛兰爱唯欧.txt',\n",
       " '雪佛兰科帕奇.txt',\n",
       " '雪佛兰科鲁兹.txt',\n",
       " '雪佛兰赛欧.txt',\n",
       " '雪佛兰迈锐宝.txt',\n",
       " '雪铁龙C2.txt',\n",
       " '雪铁龙C4L.txt',\n",
       " '雪铁龙C5.txt',\n",
       " '雪铁龙世嘉.txt',\n",
       " '雪铁龙凯旋.txt',\n",
       " '雪铁龙爱丽舍.txt',\n",
       " '雷克萨斯CT.txt',\n",
       " '雷克萨斯ES.txt',\n",
       " '雷克萨斯IS.txt',\n",
       " '雷克萨斯RX经典.txt',\n",
       " '雷诺科雷傲.txt',\n",
       " '马自达2.txt',\n",
       " '马自达2劲翔.txt',\n",
       " '马自达3.txt',\n",
       " '马自达3星骋.txt',\n",
       " '马自达5.txt',\n",
       " '马自达6.txt',\n",
       " '马自达睿翼.txt',\n",
       " '马自达阿特兹.txt',\n",
       " '宝来经典.txt']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_txt('./model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "if '雪佛兰乐驰'+'.txt' in file_txt('./model'):\n",
    "    clf = joblib.load(\"./model/\"+'雪佛兰乐驰'+'.m')\n",
    "\n",
    "    with open(\"./model/\"+'雪佛兰乐驰'+'.txt', 'r') as f1:\n",
    "        list1 = f1.readline()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "58.78"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime\n",
    "def car2price(mileage, time, brand):\n",
    "    result = 0\n",
    "    if brand+'.txt' in file_txt('./model'):\n",
    "        clf = joblib.load(\"./model/\"+brand+'.m')\n",
    "\n",
    "        with open(\"./model/\"+brand+'.txt', 'r') as f1:\n",
    "            list1 = f1.readline()\n",
    "        time_subtract = (datetime.now() - datetime.strptime(time,\"%Y-%m\")).days/365\n",
    "        temp = pd.DataFrame([mileage,time_subtract,float(list1)]).transpose()\n",
    "        temp.columns = ['mileage', 'time_subtract', 'price_y']\n",
    "        result = np.exp(clf.predict(temp))-1\n",
    "    else:\n",
    "        result = None\n",
    "    return result\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  1  2  3"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.DataFrame([1,2,3]).transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5187007]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car2price(9.3,'2012-3','比亚迪F3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8.21085491]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15072955440948932"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(22.31264606 -19.39)/19.39"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 9, 26, 17, 53, 12, 730819)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2013, 5, 1, 0, 0)"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.strptime('2013-5',\"%Y-%m\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.4082191780821915"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(datetime.now() - datetime.strptime('2013-5',\"%Y-%m\")).days/365"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
